<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../lib/Boostsrap/boostsrap.css">
    <script src="../lib/JQuery/jquery.js"></script>
</head>

<body style="padding: 20px;">
    <!-- 添加图书面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加新图书</h3>
        </div>
        <div class="panel-body form-inline">
            <!-- 框一 -->
            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
            </div>
            <!-- 2 -->
            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
            </div>
            <!-- 3 -->
            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
            </div>
            <!-- 添加按钮 -->
            <button id="btnAdd" class="btn btn-primary">添加</button>

        </div>
    </div>

    <!-- 图书表格 -->

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb"></tbody>
    </table>


    <script>
        $(function () {
            // 获取图书数据
            function getBooklist() {
                $.ajax({
                    type: "GET",
                    url: "http://www.liulongbin.top:3006/api/getbooks",
                    data: "",
                    success: function (res) {
                        console.log(res);
                        if (res.status != 200) return alert("获取失败")
                        // 创建数组
                        var rows = []
                        $.each(res.data, function (i, item) {
                            rows.push('<tr><td>' + item.id + '</td><td>' + item.bookname +
                                '</td><td>' + item.author + '</td><td>' + item
                                .publisher +
                                '</td><td><a href="javascript:;" class="del" data-id="' +
                                item.id + '">删除</a></td></tr>'
                            )
                        })
                        // empty 清空   append 追加内容
                        $('#tb').empty().append(rows.join(''))
                    }
                })
            }
            // 调用函数
            getBooklist()

            // 利用body 代理del 点击删除事件
            $("#tb").on("click", ".del", function () {
                var id = $(this).attr("data-id") //获取当前点击 自定义的id
                // console.log(id);
                $.get('http://www.liulongbin.top:3006/api/delbook', {
                    id: id
                }, function (res) {
                    if (res.status !== 200) return alert('删除失败')
                    getBooklist()
                    console.log(res);
                })
            })

            // 添加数据
            $("#btnAdd").on("click", function () {
                var bookname = $("#iptBookname").val().trim() // 去除开头和结尾的空格
                var author = $("#iptAuthor").val().trim()
                var publisher = $("#iptPublisher").val().trim()
                if (bookname.length <= 0 || author.length <= 0 || publisher.length <= 0) {
                    return alert('请填写完整信息！')
                }
                // alert('ok')
                // 发送图书数据
                $.post("http://www.liulongbin.top:3006/api/addbook", {
                    bookname: bookname,
                    author: author,
                    publisher: publisher
                }, function (res) {
                    if (res.status !== 201) return alert('添加失败！！') // 判断是否成功
                    // 重新调用
                    getBooklist()
                    // 清空内容
                    $("#iptBookname").val('')
                    $("#iptAuthor").val('')
                    $("#iptPublisher").val('')
                })
            })
        })
    </script>

</body>

</html>